package com.yowasa.xiuxian.engine.message.hone;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.engine.HoneEngine;
import com.yowasa.xiuxian.engine.hone.MentalInterface;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.enums.MapEnum;
import com.yowasa.xiuxian.entity.enums.StageEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.SacrificeLog;
import com.yowasa.xiuxian.entity.po.XiuXianExploreUser;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.ExploreHelper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.service.helper.UserTagHelper;
import com.yowasa.xiuxian.util.DateXUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.MessageFormat;
import java.util.Date;
import java.util.List;

@CMD(name = "/神樱大祓", scope = CmdScopeEnum.ALL)
public class SacrificeHandler extends CmdMessageHandler {

    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private ExploreHelper exploreHelper;

    @Override
    @Transactional
    public void execute(SendMessage message) {
        UserBO user = userHelper.checkOpUserAll(message);
        MapEnum map = MapEnum.getById(user.getLocal());
        if (user.getLocal() != 20) {
            throw new HandlerException("神樱大祓仪式必须在鸣神岛举行");
        }
        List<String> params = getAllParam(message);//获取名称
        if (StringUtils.isEmpty(params.get(0))) {
            messageHelper.replyMys(message, "可以在后面追加 修为 道行 业力等二级指令进行后续操作\n" +
                    "请使用 /帮助 /神樱大祓 查看具体规则");
            return;
        }
        SacrificeLog sacrificeLog = new SacrificeLog();
        sacrificeLog.setAccountId(user.getId());
        sacrificeLog.setStage(user.getStage());
        sacrificeLog.setUpdateTime(new Date());
        //        INSERT INTO `xiuxian_bak`.`xiuxian_sacrifice_log`(`account_id`, `update_time`, `type`, `last_gold`, `cost_gold`) VALUES (2, '2024-02-02 15:34:12', 1, 300, 100)
        XiuXianExploreUser exploreUser = exploreHelper.selectUser(user.getId());
        if ("修为".equals(params.get(0))) {
            Integer level = user.getLevel();
            if(level >= 100){
                throw new HandlerException("当前境界已修炼圆满，无需通过神樱大祓辅助修炼");
            }
            StageEnum byId = StageEnum.getById(user.getStage());
            //1%所需的雷印
            Integer needGold = byId.getNeed() / 5;
            Integer count = exploreHelper.countSacrificeByStage(user.getId(),user.getStage());
            if(count >= 5){
                throw new HandlerException("修行要稳扎稳打，不能拔苗助长哦，当前境界提升修为次数达到上限，不可以再提升了");
            }
            count++;
            needGold *= count;
            if(exploreUser.getGoldNum() < needGold){
                throw new HandlerException("本次提升需要"+needGold+"个雷之印，你当前只有"+exploreUser.getGoldNum());
            }
            exploreUser.setGoldNum(exploreUser.getGoldNum() - needGold);
            user.setLevel(Math.min(user.getLevel() + 2*count,100));
            sacrificeLog.setCostGold(needGold);
            sacrificeLog.setLastGold(exploreUser.getGoldNum());
            sacrificeLog.setType(1);
            messageHelper.replyMys(message, "你消耗了"+needGold+"个雷之印，增长了"+(2*count)+"%的修为");
        }

        if ("道行".equals(params.get(0))) {
            if(exploreUser.getGoldNum() < 1000){
                throw new HandlerException("每次降低道行需要消耗1000雷之印");
            }

            if(user.getUserConfig().getAbility() <= 0){
                throw new HandlerException("你当前道行为0，无须降低道行");
            }
            Integer count = exploreHelper.countSacrificeByWeek(user.getId());
            if(count >= 5){
                throw new HandlerException("本周已经降低了五次道行了！不可再接受神樱大祓了");
            }
            exploreUser.setGoldNum(exploreUser.getGoldNum() - 1000);
            user.getUserConfig().setAbility(user.getUserConfig().getAbility() - 2);
            sacrificeLog.setCostGold(1000);
            sacrificeLog.setLastGold(exploreUser.getGoldNum());
            sacrificeLog.setType(2);
            messageHelper.replyMys(message, "你消耗了1000雷之印，降低了2点道行");
        }

        if ("业力".equals(params.get(0))) {
            int i = exploreUser.getKarma() / 10;
            Integer needGold = (i+1) * 50;
            if(exploreUser.getGoldNum() < needGold){
                throw new HandlerException("洗去业力需要消耗"+needGold+"雷之印");
            }

            if(exploreUser.getKarma() <= 0){
                throw new HandlerException("你当前业力为0，无须洗去业力");
            }

            exploreUser.setGoldNum(exploreUser.getGoldNum() - needGold);
            exploreUser.setKarma(exploreUser.getKarma() - 1);
            sacrificeLog.setCostGold(needGold);
            sacrificeLog.setLastGold(exploreUser.getGoldNum());
            sacrificeLog.setType(3);
            messageHelper.replyMys(message, "你消耗了"+needGold+"雷之印，洗去了一点业力");
        }
        if ("修炼".equals(params.get(0))){

            Integer level = user.getLevel();
            if(level >= 100){
                throw new HandlerException("当前境界已修炼圆满，无需通过神樱大祓辅助修炼");
            }
            if(exploreUser.getGoldNum() < 200){
                throw new HandlerException("时间加速一年需要200雷之印");
            }

            MentalInterface mental = HoneEngine.mentalMap.get(user.getUserConfig().getMental());
            int exp = mental.getExp(user, 360);
            userHelper.addExp(user, exp);
            exploreUser.setGoldNum(exploreUser.getGoldNum() - 200);
            sacrificeLog.setCostGold(200);
            sacrificeLog.setLastGold(exploreUser.getGoldNum());
            sacrificeLog.setType(4);
            userHelper.lockUser(user,25,"正在进行神樱大祓仪式");
            messageHelper.replyMys(message, "你消耗了200雷之印，进入了时间加速状态");
        }
        exploreHelper.updateUser(exploreUser);
        exploreHelper.insertSacrifice(sacrificeLog);
        userHelper.saveUserInfo(user);
    }

    public String desc(){
        return new StringBuffer().append("神樱大祓\n")
                .append("神樱大社的巫女花散里，借助神樱树的力量为你进行祓除仪式\n")
                .append("修为：每个境界可进行五次仪式，每次提升 2%/4%/6%/8%/10% 的修为，每次所需雷印也变多\n")
                .append("修炼：无限制，200雷印兑换一次加速修炼,只需修炼5分钟，便可获得一年的修为\n")
                .append("道行：每周仅能洗五次，每次1000雷印洗去2点\n")
                .append("业力：无限制，一次一点，0-10业力，每次消耗50，10-20业力，每次消耗100 以此类推")
                .toString();
        //  100  200  400  600  800
        // 4   5    6    7    8    9
        //乌 青 白 - 金 - 铂 - 钻 - 星 - 尘


        // 800
    }
}
